/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.resources;

import java.io.InputStream;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.ILogEntry;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class RemoteFile extends RemoteResource {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the depot filename. */
  private String depotFileName;
  /** Stores the revision. */
  private String revision;
  /** Stores the resource sync info. */
  private ResourceSyncInfo resourceSyncInfo;

  // --------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for RemoteFile.
   * 
   * @param server the perforce server
   * @param depotFileName the depot filename.
   * @param revision the revision.
   */
  public RemoteFile(IPerforceServer server, String depotFileName, String revision) {
    this.depotFileName = depotFileName;
    this.revision = revision;

    setServer(server);
  }

  /**
   * Constructor for RemoteFile.
   * 
   * @param server the perforce server
   * @param syncInfo the resource's sync info.
   * @param revision the revision.
   */
  public RemoteFile(IPerforceServer server, ResourceSyncInfo syncInfo, String revision) {
    setServer(server);

    this.resourceSyncInfo = syncInfo;
    this.revision = revision;
    this.depotFileName = syncInfo.getDepotFileName();
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public RemoteResource find(String name, IProgressMonitor monitor) {
    if (name.equals(getDepotFilename())) {
      return this;
    }

    return null;
  }

  @Override
  public InputStream getContents(IProgressMonitor progress) throws PerforceException {
    return getServer().print(depotFileName, revision);
  }

  @Override
  public String getName() {
    int start = depotFileName.lastIndexOf('/');
    return depotFileName.substring(start + 1);
  }

  @Override
  public boolean isContainer() {
    return false;
  }

  @Override
  public ResourceSyncInfo getSyncInfo(IProgressMonitor monitor) {
    try {
      if (resourceSyncInfo == null) {
        ResourceSyncInfo[] resourceSyncInfos = getServer().fstat(depotFileName + revision, monitor);
        if (resourceSyncInfos.length == 1) {
          resourceSyncInfo = resourceSyncInfos[0];
        }
      }

      return resourceSyncInfo;
    } catch (PerforceException e) {
      PerforceCorePlugin.log(e.getStatus());
    }

    return null;
  }

  @Override
  public RemoteResource[] members(IProgressMonitor progress) {
    return new RemoteResource[0];
  }

  /**
   * Returns the depot filename.
   * 
   * @return the depot file name.
   */
  @Override
  public String getDepotFilename() {
    ResourceSyncInfo info = getSyncInfo(null);
    if (info == null) return null;
    return info.getDepotFileName();
  }

  public ILogEntry[] getLogEntries(boolean followBranches, IProgressMonitor monitor)
      throws TeamException {
    return getServer().filelog(this, 25, followBranches);
  }

  /**
   * Checks whether the remote file is deleted.
   * 
   * @return true if remote is deleted.
   */
  public boolean isDeleted() {
    return getSyncInfo(null).isDeleted();
  }

  /**
   * Returns the revision of the file.
   * 
   * @return the revision as string.
   */
  public String getRevision() {
    return revision;
  }

  public String getComment() {
    // TODO Auto-generated method stub
    return null;
  }

  public String getContentIdentifier() {
    // TODO Auto-generated method stub
    return null;
  }

  public String getCreatorDisplayName() {
    // TODO Auto-generated method stub
    return null;
  }

  public IStorage getBufferedStorage(IProgressMonitor monitor) {
    // TODO Auto-generated method stub
    return null;
  }
}
